<?php

namespace App\Modules\Admin\Services\Equipment;

use App\Library\Junda\src\EquipmentService;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;
use App\Exceptions\Exception;

class ApparatusJdService extends BaseService
{
    public function __construct(WaterDeviceModel $apparatus)
    {

    }

    public  function getJundaSelectlists()
    {
        $list = EquipmentService::GetEquipmentList();
        return $list['rows'];
    }
    public  function getOnlineStatusByMid($params)
    {$mid = $params['mid'];
        $list = EquipmentService::GetOnlineStatusByMid($mid);
        return $list;
    }

    public function createJd(array $params)
    {
        $mid = $params['mid'];
        $location = $params['location']??"35.234556,118.345234";
        $rich_title = $params['title'];
        DB::beginTransaction();
        try{
            $response_mineral = EquipmentService::AddEquipment($mid,$rich_title,$location);
            if ($response_mineral['success']==false){
                $this->error='修改失败，设备不存在，设备编号【'.$mid.'】';
                DB::rollBack();
                return false;
            }
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    public function updateJd(array $params)
    {
        $mid = $params['mid'];
        $rich_title = $params['title'];

        DB::beginTransaction();
        try{
            $response_mineral = EquipmentService::UpdateEquipName($mid,$rich_title);
            if ($response_mineral['success']==false){
                $this->error='修改失败，设备不存在，设备编号【'.$mid.'】';
                DB::rollBack();
                return false;
            }
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }

}
